package cn.edu.njuit.server.ugcnote.ugcnoteserver.apis;

import cn.edu.njuit.server.ugcnote.ugcnoteserver.entity.NoteInfo;
import cn.edu.njuit.server.ugcnote.ugcnoteserver.entity.TypeInfo;
import cn.edu.njuit.server.ugcnote.ugcnoteserver.service.NoteInfoService;
import cn.edu.njuit.server.ugcnote.ugcnoteserver.service.TypeInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/open")
public class OpenApi {
    @Autowired
    TypeInfoService typeInfoService;
    @Autowired
    NoteInfoService noteInfoService;

    @GetMapping("/hello")
    public ResponseEntity Hello(){
        return ResponseEntity.ok("Hello world.");
    }

    @GetMapping("/types")
    public ResponseEntity getAllTypes(){
        List<TypeInfo> types=this.typeInfoService.getAllTypes();
        return ResponseEntity.ok(types);
    }

    @GetMapping("/notes")
    public ResponseEntity getAllNotesByType(
            @RequestParam(required = false,defaultValue = "0") Long type,
            @RequestParam(required = false,defaultValue = "0") Integer page,
            @RequestParam(required = false,defaultValue = "10") Integer size){
        Page<NoteInfo> noteInfoList=this.noteInfoService.findAllNotes(type,page,size);
        return ResponseEntity.ok(noteInfoList);
    }
    @GetMapping("/notes/{nodeId}")
    public ResponseEntity getOneNote(@PathVariable Long nodeId){
        NoteInfo result=this.noteInfoService.getNoteInfo(nodeId);
        return ResponseEntity.ok(result);
    }
}
